package club.ruanx.auth.vcode.image;

import club.ruanx.auth.vcode.exception.ValidateCodeException;
import club.ruanx.auth.vcode.factory.AbstractValidateCodeProcessor;
import club.ruanx.util.ServletContextUtils;
import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 阮胜
 * @date 2018/9/30 13:11
 */
@Component
public class ImageCodeProcessor extends AbstractValidateCodeProcessor<ImageCode> {

    @Override
    protected void send(ImageCode imageCode) throws ValidateCodeException, IOException {
        HttpServletResponse resp = ServletContextUtils.getCurrentResponse();
        ImageIO.write(imageCode.getImage(), "JPEG", resp.getOutputStream());
    }
}
